﻿using System.Collections.ObjectModel;
using System.Linq;

namespace Central.Linq.MvcJson.Engine {

	public class JsonFilterManager {

		public JsonFilterManager() {
			this.FilterParams = new Collection<FilterParam>();
			this.JsonFilterManagers = new Collection<JsonFilterManager>();
		}

		public LogicalOperator LogicalOperator { get; set; }

		public Collection<FilterParam> FilterParams { get; set; }

		public Collection<JsonFilterManager> JsonFilterManagers { get; set; }

		internal FilterManager CreateFilterManager {
			get {
				if (this.FilterParams != null && this.FilterParams.Count > 0) {
					FilterManager result = new FilterManager(this.LogicalOperator);
					foreach (FilterParam fd in FilterParams) {
						result.Add(new FilterDefinition { Id = fd.Id, Name = fd.Name, Operator = fd.Operator, Value = fd.Value });
					}
					if (this.JsonFilterManagers != null && this.JsonFilterManagers.Count() > 0) {
						foreach (JsonFilterManager item in JsonFilterManagers) {
							result.AddChild(item.CreateFilterManager);
						}
					}
					return result;
				}
				else {
					return null;
				}
			}
		}
	}
}